/**
 * Actions.java
 * @author: liuex
 * 2008-3-15 ����11:57:41
 *
 * note: 
 */
package org.lex.swing;

import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.KeyStroke;

public class ActionHelper {
	public static final String[] PROPERTIES = { Action.ACCELERATOR_KEY, Action.ACTION_COMMAND_KEY, Action.DEFAULT,
			Action.DISPLAYED_MNEMONIC_INDEX_KEY, Action.LARGE_ICON_KEY, Action.LONG_DESCRIPTION, Action.MNEMONIC_KEY, Action.NAME,
			Action.SELECTED_KEY, Action.SHORT_DESCRIPTION, Action.SMALL_ICON, };

	/**
	 * The key used for storing a <code>KeyStroke</code> to be used as the
	 * accelerator for the action.
	 */
	public static KeyStroke getAccelerator(Action action) {
		return (KeyStroke) action.getValue(Action.ACCELERATOR_KEY);
	}

	public static Action setAccelerator(Action action, KeyStroke value) {
		action.putValue(Action.ACCELERATOR_KEY, value);
		return action;
	}

	public static String getActionCommand(Action action) {
		return (String) action.getValue(Action.ACTION_COMMAND_KEY);
	}

	public static Action setActionCommand(Action action, String value) {
		action.putValue(Action.ACTION_COMMAND_KEY, value);
		return action;
	}

	public static Icon getLargeIcon(Action action) {
		return (Icon) action.getValue(Action.LARGE_ICON_KEY);
	}

	public static Action setLargeIcon(Action action, Icon value) {
		action.putValue(Action.LARGE_ICON_KEY, value);
		return action;
	}

	public static String LongDescription(Action action) {
		return (String) action.getValue(Action.LONG_DESCRIPTION);
	}

	public static Action setLongDescription(Action action, String value) {
		action.putValue(Action.LONG_DESCRIPTION, value);
		return action;
	}

	public static int getMnemonic(Action action) {
		return (Integer) action.getValue(Action.MNEMONIC_KEY);
	}

	public static Action setMnemonic(Action action, int value) {
		action.putValue(Action.MNEMONIC_KEY, value);
		return action;
	}

	public static String getName(Action action) {
		return (String) action.getValue(Action.NAME);
	}

	public static Action setName(Action action, String value) {
		action.putValue(Action.NAME, value);
		return action;
	}

	public static boolean isSelected(Action action) {
		return (Boolean) action.getValue(Action.SELECTED_KEY);
	}

	public static Action setSelected(Action action, boolean value) {
		action.putValue(Action.SELECTED_KEY, value);
		return action;
	}

	public static String getShortDescription(Action action) {
		return (String) action.getValue(Action.SHORT_DESCRIPTION);
	}

	public static Action setShortDescription(Action action, String value) {
		action.putValue(Action.SHORT_DESCRIPTION, value);
		return action;
	}

	public static Icon getSmallIcon(Action action) {
		return (Icon) action.getValue(Action.SMALL_ICON);
	}

	public static Action setSmallIcon(Action action, Icon value) {
		action.putValue(Action.SMALL_ICON, value);
		return action;
	}

	public static boolean isEnabled(Action action) {
		return action.isEnabled();
	}

	public static Action setEnabled(Action action, boolean value) {
		action.setEnabled(value);
		return action;
	}

	public static Action set(Action action, String name, int mnemonic, KeyStroke ks) {
		if (null != name)
			ActionHelper.setName(action, name);
		if (mnemonic > 0)
			ActionHelper.setMnemonic(action, mnemonic);
		if (null != ks)
			ActionHelper.setAccelerator(action, ks);
		return action;
	}

	public static String toString(Action a) {
		StringBuilder buf = new StringBuilder();
		buf.append("Action Class: ");
		buf.append(a.getClass().getCanonicalName()).append(", ");
		for (String p : PROPERTIES) {
			Object value = a.getValue(p);
			if (null != value) {
				buf.append("Attribute= '").append(p);
				buf.append("', Class= ");
				buf.append(value.getClass().getCanonicalName());
				buf.append(", value= ").append(value);
				buf.append("; ");
			}
		}
		buf.append('\n');
		return buf.toString();
	}

	private ActionHelper() {
	}
}
